from selenium.webdriver.common.by import By

from page.start_page import StartPage
from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException
from time import sleep
from common.user_data import project_name as p_name


class MainPage(StartPage):


    def enter_project(self, project_name=p_name):
        # self.find_element(*self.project_btn).click()
        sleep(4)

        for i in range(5):
            try:
                ele = self.find_static_text(string=project_name, contains_flag=1)
                ele.click()
                # self.find_static_text(string=project_name, contains_flag=5).click()
            except NoSuchElementException:
                self.swipe(40, 470, 40, 440)
                sleep(2)
                # self.find_static_text(string=project_name, contains_flag=1, times=5).click()
                continue
            else:
                break
        sleep(2)

    def enter_live_platform(self):
        sleep(3)
        self.find_button("直播").click()
        sleep(3)

    def back_to_main(self):
        # self.get_back().click()
        # self.find_xpath('//*[@name="top返回White"]').click()
        # self.find_button('top返回').click()
        # self.find_button('top返回White').click()
        for i in range(5):
            try:
                  self.find_button(string="返回", contains_flag=1, times=2).click()
                 # self.find_xpath('//*[@name="top返回"]').click()
            except NoSuchElementException:
                break
            except ElementNotVisibleException:
                break
            else:
                continue
        for i in range(5):
            try:
                 self.find_image(string="返回", attr='name', contains_flag=1, times=2).click()
                # self.find_xpath('//*[@name="top返回White"]').click()
            except NoSuchElementException:
                break
            except ElementNotVisibleException:
                break
            else:
                continue

    def logout(self):
        # sleep(2)
        # self.back_to_main()
        sleep(2)
        self.find_button("我").click()
        self.find_button("icon 设置copy").click()
        sleep(1)
        # self.find_xpath('//XCUIElementTypeNavigationBar[@name]/XCUIElementTypeButton').click()  # 点击设置按钮
        self.find_static_text("退出登录").click()
        sleep(1)
        # self.find_xpath('//*[@name="退出登录"]')
        # self.__class__('XCUIElementTypeOther').click()
        self.find_button("确定").click()


if __name__ == '__main__':
    test = MainPage()
    test.enter_project()
